package com.zyj.springboothapim.controller;

import com.zyj.springboothapim.bean.Admin;
import com.zyj.springboothapim.service.AdminService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
//这个是默认json接收和发送@RestController
@RequestMapping("/Admin")
public class AdminController {


//    Logger logger = LoggerFactory.getLogger(HtmlController.class);
    AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService)
    {
        this.adminService = adminService;
    }

    //通过工号查询管理员信息
    @RequestMapping(value = "/selectAdminByid",method = RequestMethod.POST)
    @ResponseBody
    public Admin selectAdminByid(@RequestBody Admin admin){

        return adminService.getAdiminById(admin);
    }

    //通过session记录登录状态工号查询管理员信息
    @RequestMapping(value = "/selectAdminBySessionId",method = RequestMethod.POST)
    @ResponseBody
    public Admin selectAdminBySessionId(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        System.out.println("/Admin/selectAdminBySessionId");
        HttpSession session = request.getSession();
        Admin admin= (Admin) session.getAttribute("admin");
        System.out.println(admin.getAdmin_id() + "===========-*-*");
//        Admin admin = new Admin();
//        admin.setAdmin_id();
        return adminService.getAdiminById(admin);
    }
    //查询全部员工
    @RequestMapping(value = "/selectAdmin" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<Admin> selectAdmin(){
        System.out.println("/Admin/selectAdmin");
        return adminService.selectAdimin();
    }

    //登录验证
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody Admin admin, HttpServletRequest request, HttpServletResponse response, ModelMap model){
        System.out.println(admin.getAdmin_id() + "    *-*-*-*--");
        System.out.println(admin.getAdmin_password() + "    *-*-*-*--");
        String returnStr = adminService.login(admin);

        //设置了cookie和session
        Cookie cookie=new Cookie("admin_id",""+ admin.getAdmin_id());
        cookie.setMaxAge(1*24*60*60);     //存活一天
        response.addCookie(cookie);

        HttpSession session = request.getSession();
        session.setAttribute("admin", admin);


        /**
         * returnStr = "fail,no AdminId"
         * returnStr = "success,index.html"
         * returnStr = "fail,password error"
         * */
        return returnStr;
    }

    //通过工号修改管理员信息
    @RequestMapping(value = "/updataAdmin",method = RequestMethod.POST)
    @ResponseBody
    public String updataAdmin(@RequestBody Admin admin)
    {
        adminService.updataAdmin(admin);
        return "update success";
    }

//    public Map<String,Object> map(){
//        List<Map<String, Object>> list = jdbcTemplate.queryForList("select  * FROM  admin_table");
//        return list.get(0);
//    }

}
